package com.wy.elasticsearch;

import org.elasticsearch.index.query.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class ElasticSearchService {

    @Autowired
    private BaseInfoInterface infoInterface;

    @Autowired
    private ElasticsearchTemplate template;

    public List<BaseInfo> findAll(){
        List<BaseInfo> list = new ArrayList<>();
        Iterable<BaseInfo> all = infoInterface.findAll();
        all.forEach(baseInfo ->list.add(baseInfo) );

        return list;
    }

    public void save(){}


    /***
     * 模糊查询 相当于sql的 where username like '%王五%'
     * @param name
     * @return
     */
    public Page<BaseInfo> testFuzzyQuery(String name){
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        builder.withQuery(QueryBuilders.matchPhraseQuery("username",name));



        int pageNo = 0;
        int pageSize = 10;
        builder.withPageable(PageRequest.of(pageNo,pageSize));

        Page<BaseInfo> page = this.infoInterface.search(builder.build());
        for(BaseInfo item:page){
            System.out.println(item);
        }

        return page;
    }

    /***
     * 左前缀查询 相当于sql的 where regioncode like "3714%"
     * @param regionCode
     * @return
     */
    public Page<BaseInfo> startWith (String regionCode){
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        builder.withQuery(QueryBuilders.matchPhrasePrefixQuery("regioncode",regionCode));

        int pageNo = 0;
        int pageSize = 10;
        builder.withPageable(PageRequest.of(pageNo,pageSize));


        Page<BaseInfo> page = this.infoInterface.search(builder.build());
        for(BaseInfo item:page){
            System.out.println(item);
        }

        return page;
    }

    /***
     * 精确查询 相当于sql的 where birthyear=1991
     * @param birthYear
     * @return
     */
    public Page<BaseInfo> termQuery (String birthYear){
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        builder.withQuery(QueryBuilders.termQuery("birthyear",birthYear));

        int pageNo = 0;
        int pageSize = 10;
        builder.withPageable(PageRequest.of(pageNo,pageSize));


        Page<BaseInfo> page = this.infoInterface.search(builder.build());
        for(BaseInfo item:page){
            System.out.println(item);
        }

        return page;
    }

    /***
     * 范围查询 相当于sql的 where jointime between 1991 and 1999
     * ****需要传入时间戳 否则会报错****
     * @param start
     * @param end
     * @return
     */
    public Page<BaseInfo> rangeQuery (Date start, Date end){
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        RangeQueryBuilder birthyear = QueryBuilders.rangeQuery("jointime");
        birthyear.gte(start.getTime());
        birthyear.lte(end.getTime());
        builder.withQuery(birthyear);

        int pageNo = 0;
        int pageSize = 10;
        builder.withPageable(PageRequest.of(pageNo,pageSize));


        Page<BaseInfo> page = this.infoInterface.search(builder.build());
        for(BaseInfo item:page){
            System.out.println(item);
        }

        return page;
    }

    public void deleteIndex(){
        template.deleteIndex(BaseInfo.class);
    }

    /***
     * 多条件组合查询 相当于sql的 where a=1 and b=2 ...
     * @return
     * @throws Exception
     */
    public Page<BaseInfo> multiConditionalQuery() throws Exception{
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        MatchPhrasePrefixQueryBuilder matchPhrasePrefixQuery = QueryBuilders.matchPhrasePrefixQuery("regioncode", "14");
        TermQueryBuilder termQuery = QueryBuilders.termQuery("birthyear", 1972);
        RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("jointime");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        rangeQuery.gte(sdf.parse("1990-01-01").getTime());
        rangeQuery.lte(sdf.parse("1995-01-01").getTime());
        MatchPhraseQueryBuilder phraseQuery = QueryBuilders.matchPhraseQuery("username", "王勇");

        boolQuery.must(matchPhrasePrefixQuery)
                .must(termQuery)
                .must(rangeQuery)
                .must(phraseQuery);

        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        builder.withQuery(boolQuery);

        int pageNo = 0;
        int pageSize = 10;
        builder.withPageable(PageRequest.of(pageNo,pageSize));

        Page<BaseInfo> page = this.infoInterface.search(builder.build());
        for(BaseInfo item:page){
            System.out.println(item);
        }

        return page;
    }
}
